package com.example.c201901090104;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;

import java.util.List;

public class Register extends AppCompatActivity {
    UserDao userDao;
    UserDatabase userDatabase;
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        userDatabase= Room.databaseBuilder(this,UserDatabase.class,"myuser1").allowMainThreadQueries().build();
        userDao=userDatabase.getUserDao();
        StatusBarUtil.setColor(this,getColor(R.color.white),0);
        findViewById(R.id.textView16).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Register.this,Login.class);
                startActivity(intent);
                Register.this.finish();
            }
        });
        findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText=findViewById(R.id.editTextTextPersonName2);
                String id=editText.getText().toString();
                EditText editText1=findViewById(R.id.editTextTextPassword2);
                String pd=editText1.getText().toString();
                EditText editText2=findViewById(R.id.editTextTextPassword3);
                String pd2=editText2.getText().toString();
                if(id.equals("")){
                    Toast.makeText(Register.this,"请输入账号",Toast.LENGTH_SHORT).show();
                }
                else if(pd.equals("")){
                    Toast.makeText(Register.this,"请输入密码",Toast.LENGTH_SHORT).show();
                }
                else if(!pd.equals(pd2)){
                    Toast.makeText(Register.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                }
                else {
                    List<User> userList=userDao.findUserById(id);
                    if(userList.size()==0){
                        User user=new User();
                        user.id=id;
                        user.password=pd;
                        userDao.insert(user);
                        Toast.makeText(Register.this,"注册成功",Toast.LENGTH_SHORT).show();
                        SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putBoolean("success",true);
                        editor.commit();
                        Intent intent=new Intent(Register.this,MainActivity.class);
                        startActivity(intent);
                        Register.this.finish();
                    }
                    else {
                        Toast.makeText(Register.this,"账号已存在",Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });
    }
}